Ext.define('DCApp.controller.ListController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.list',

    getSelectedItem: function(){
        return this.getView().getSelectionModel().getSelection()[0] || false;
    },

     onNew: function() {
     	var me=this;
		var obj = me.getView().getStore().getModel().create();
    	obj.save({
    	success:function(rec) {
    		me.fireViewEvent('openobj', me.getView(), rec);
    	}});
    },

    onEdit: function(view, rec) {
    	var vw = this.getView();
    	if(!vw.isXType('grid')){
    		vw = vw.down('dataview');
    	}
    	var rec = vw.getSelectionModel().getSelection()[0] ;
		this.fireViewEvent('editobj',vw , rec);
    },
    onDblClick: function(view, rec) {
		this.fireViewEvent('openobj', this.getView(), rec);
    },

    onRefreshClick: function() {
        this.getView().getStore().load();
    },
    onRemove:function(){
    	var vw = this.getView();
    	//for dataview
    	if(!vw.isXType('grid')){
    		vw = vw.down('dataview');
    	}

		var sels = vw.getSelectionModel().getSelection();
		Ext.MessageBox.confirm('确认删除', '您确定要删除选中的[' + sels.length + ']项?',
			function(btn, text) {
				if (btn != 'yes')
					return false;
				// 组织参数
				var relId = [];
				for(var i=0;i<sels.length;i++) {
					relId[i] = sels[i].get(sels[i].idProperty);
				}
				var idProperty = sels[0].idProperty;
				var relEntity = sels[0].entityName;
				Ext.Ajax.request({
		            url: '/api/remove',
		            method: 'POST',
		            headers: { 'Content-Type': 'application/json' },
		            params : Ext.JSON.encode({'id':relId,idProperty:idProperty,'entity':relEntity}),
		            success: function(conn, response, options, eOpts) {
		            	//vw.getStore().getData().remove(sels);
		            	vw.getStore().load();
		            },
		            failure: function(conn, response, options, eOpts) {

		            }
		        });

		});
    },
    onRelAdd:function(test) {
    	var vw = this.getView();
		var sels = vw.getSelectionModel().getSelection();
		if (sels.length == 0) {
			Ext.MessageBox.alert('操作提示', '请先选择待关联的条目', null, this);
			return;
		}
		// 获取参数
		var id = vw.ds.filters.items[0]._value;
		var property = vw.ds.filters.items[0]._property;
		var entity = property.substring(0, property.length-2).toUpperCase();
		var relId = [];
		for(var i=0;i<sels.length;i++) {
			relId[i] = sels[i].get(sels[i].idProperty);
		}
		var relEntity = sels[0].entityName.toUpperCase();
		// 调用增加关联接口
		Ext.Ajax.request({
            url: '/rel/add',
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            params : Ext.JSON.encode({'id1':id,'entity1':entity,'id2':relId,'entity2':relEntity}),
            success: function(conn, response, options, eOpts) {
        		vw.ds.load();
            },
            failure: function(conn, response, options, eOpts) {
            }
        });
    },
    onRelDel:function() {
    	var vw = this.getView();
		var sels = vw.getSelectionModel().getSelection();
		if (sels.length == 0) {
			Ext.MessageBox.alert('操作提示', '请先选择待删除的条目', null, this);
			return;
		} else {
			Ext.MessageBox.confirm('确认删除', '您确定要删除选中的[' + sels.length + ']项?',
			function(btn, text) {
				if (btn != 'yes')
					return false;
				// 调用删除关联接口
				var id = vw.getStore().filters.items[0]._value;
				var property = vw.getStore().filters.items[0]._property;
				var entity = property.substring(0, property.length-2).toUpperCase();
				var relId = [];
				for(var i=0;i<sels.length;i++) {
					relId[i] = sels[i].get(sels[i].idProperty);
				}
				var relEntity = sels[0].entityName.toUpperCase();

				// 调用删除关联接口
				Ext.Ajax.request({
		            url: '/rel/delete',
		            method: 'POST',
		            headers: { 'Content-Type': 'application/json' },
		            params : Ext.JSON.encode({'id1':id,'entity1':entity,'id2':relId,'entity2':relEntity}),
		            success: function(conn, response, options, eOpts) {
		            	vw.getStore().load();
		            },
		            failure: function(conn, response, options, eOpts) {
		            }
		        });

			}, this);
		}
    },
    onRelList: function() {
    	var vw = this.getView();
    	var vtype;
    	//for dataview
    	if(!vw.isXType('grid')){
    		vw = vw.down('dataview');
    		vtype = vw.xtype.replace('view','list');
    	}else{
    		vtype = vw.xtype;
    	}
    	var ds = vw.getStore();

    	var vm = this.getViewModel();
    	var title = this.getView().up('[cls=nav_pos]').title;
    	var sel_win=vw.sel_win
    	if(!sel_win){
    		sel_win = new Ext.window.Window({
    			closeAction:'hide',
	        	width:600,
	        	height:400,
	        	layout:'fit',
	        	modal:true,
	        	//viewModel: vm,
	        	items:[{
	        			ds:ds,
	        			xtype:vtype,
	        			bind: {
	        		        store: '{objsel}'
	        		    },
	        			tbar: ['->',{
					        text: '增加关联',
					        handler: 'onRelAdd'
					    }]
	        		}
	        	],
	        	plain: true,
	            headerPosition: 'bottom'
	        });
	        vw.sel_win=sel_win;
    	}
    	sel_win.show();
    }

});
